<template>
  <div class="app-container">
    <el-row>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm el-form-bottom">
        <el-divider content-position="left"><legend class="divider-line">商城公告添加</legend></el-divider>
        <el-col :span="8">
          <el-form-item label="标题" prop="title">
            <el-input v-model="ruleForm.title"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="是否置顶" prop="isTop">
            <el-radio-group v-model="ruleForm.isTop">
              <el-radio v-for="item in statesList" :key="item.value" :label="item.value">{{ item.name }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>

        <el-col :span="20">
          <el-form-item label="排序" prop="sort">
            <el-input-number v-model="ruleForm.sort" :min="0" :max="99" :step="1" step-strictly></el-input-number>
            <span class="form-word-hint">序号越小越靠前显示</span>
          </el-form-item>
        </el-col>

        <el-col :span="16">
          <el-form-item label="简介" prop="describe">
            <el-input :autosize="{ minRows: 3 }" v-model="ruleForm.describe" type="textarea"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="内容" prop="content">
            <editor v-model="ruleForm.content" :height="300" />
          </el-form-item>
        </el-col>
      </el-form>
      <div class="fixed-bottom-20">
        <el-button type="primary" @click="submitForm('ruleForm')">立即提交</el-button>
        <el-button type="success" @click="back">返回</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </div>
    </el-row>
  </div>
</template>
<script>
import { getCodeManager, codeToArray } from "@/api/all";
import { createNoticeSeller } from "@/api/seller";
import { Message } from "element-ui";
import Editor from "@/components/Editor";
export default {
  name: "noticeadd",
  components: { Editor },
  data() {
    return {
      statesList: [],
      ruleForm: {
        title: "",
        isTop: "",
        sort: "",
        describe: "",
        content: "",
      },
      rules: {
        title: [
          { required: true, message: "请输入标题", trigger: "blur" },
          { max: 50, message: "标题长度为不能超过50个字符", trigger: "blur" },
        ],
        isTop: [{ required: true, message: "是否置顶必选", trigger: "change" }],
        describe: [{ required: true, message: "请输入简介", trigger: "blur" }],
        sort: [{ required: true, message: "请输入权重", trigger: "blur" }],
        content: [{ required: true, message: "请输入内容", trigger: "blur" }],
      },
    };
  },
  created() {
    getCodeManager("YES_NO", () => {
      this.statesList = codeToArray("YES_NO");
    });
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          createNoticeSeller(this.ruleForm).then((res) => {
            if (!res.data.success) {
              Message.error(res.data.message);
              return;
            }
            Message.success("操作成功");
            history.back();
          });
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    back() {
      history.back();
    },
  },
};
</script>
<style scoped>
/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
/deep/ .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
/deep/ [class*="el-col-"] {
  float: none;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
